﻿using System;
using System.Collections.Generic;
using System.Linq;
using RBOSS.Business.Checkout;
using RBOSS.Business.ShoppingCarts;
using RBOSS.Data;
using System.Data.Entity.Migrations;

namespace RBOSS.Business.CustomerModule
{
    public class CustomerSubSystem : ICustomerSubSystem
    {
        private readonly RBOSSEntities _rBossEntities;

        public CustomerSubSystem()
        {
            _rBossEntities = new RBOSSEntities();
        }

        public IList<CustomerOrderDetails> GetCustomerOrderDetails(int orderId)
        {
            IQueryable<CustomerOrderDetails> customerOrderDetails = from orderDetails in _rBossEntities.OrderDetails
                                                            where orderDetails.OrderId == orderId
                                                                    join product in _rBossEntities.Products on orderDetails.ProductId equals product.ProductId
                                                                    select new CustomerOrderDetails
                                                                    {
                                                                        OrderDate = orderDetails.Order.OrderDate,
                                                                        ProductName = product.ProductName,
                                                                        Quantity = orderDetails.Quantity,
                                                                        UnitPrice = (decimal)orderDetails.Price,
                                                                        TotalPrice = (decimal)orderDetails.Quantity * (decimal)orderDetails.Price
                                                                    };
            return customerOrderDetails.ToList();
        }

        public IList<CustomerOrder> GetCustomerOrders(int userId)
        {
            var customerOrders = from order in _rBossEntities.Orders
                                 where order.UserId == userId
                                 select new CustomerOrder
                                 {
                                     OrderId = order.OrderId,
                                     OrderDate = order.OrderDate,
                                     ShippingStatus = order.Shipping.ShippingStatu.StatusName,
                                 };
            return customerOrders.ToList();


        }

        public void DeleteCustomer(int id)
        {
            throw new NotImplementedException();
        }

        public void UpdateCustomer()
        {
            throw new NotImplementedException();
        }

        public int GetCustomerId(string emailAddress)
        {
            var customer = _rBossEntities.Users.Where(x => x.Email == emailAddress).FirstOrDefault();
            if (customer != null)
                return customer.UserId;
            return 0;
        }

        public bool CheckEmailAvailability(string emailAddress)
        {
            if (_rBossEntities.Users.FirstOrDefault(x => x.Email == emailAddress) == null)
                return true;
            return false;
        }

        public void RegisterCustomerInSession(string email)
        {
            if (!string.IsNullOrEmpty(email))
            {
                ICustomer customer = GetCustomerFromSession();
                customer.CustomerDetails = GetCustomerInformation(email);
                customer.IsLogin = true;
                customer.BillingInfo = GetCustomerBillingInfo(customer.CustomerDetails.CustomerId);
                customer.ShippingInfo = GetCustomerShippingInfo(customer.CustomerDetails.CustomerId);
                IShoppingCartSubsystem shoppingCartSubsystem = new ShoppingCartSubsystem();
                shoppingCartSubsystem.SaveCartToDataBase();
                shoppingCartSubsystem.RetrieveShoppingCart();

            }
            else
            {
                RegisterGuestCustomerInSession();
            }
        }

        private void RegisterGuestCustomerInSession()
        {
            ICustomer customer = GetCustomerFromSession();
            customer.CustomerDetails = new CustomerItem {CustomerId = 0};
            customer.IsGuest = true;

        }

        private IShipping GetCustomerShippingInfo(int customerId)
        {
            return new Checkout.Shipping();
        }

        private IBilling GetCustomerBillingInfo(int customerId)
        {
            return new Billing();
        }

        public ICustomer GetCustomerFromSession()
        {
            return CustomerSingleton.GetCustomer();
        }

        public void AddCustomer(string userEmail, string password, string firstName, string lastName, string phoneNo, string city, string provience, string country, string postalCode)
        {
            var user = new User
            {
                Email = userEmail,
                Country = country,
                City = city,
                FirstName = firstName,
                LastName = lastName,
                Password = password,
                PhoneNo = phoneNo,
                PostalCode = postalCode,
                Province = provience,
                RoleId = 4,
            };

            _rBossEntities.Users.AddOrUpdate(user);
            _rBossEntities.SaveChanges();
        }

        public CustomerItem GetCustomerInformation(string emailAddress)
        {
            var customer = _rBossEntities.Users.FirstOrDefault(x => x.Email == emailAddress);
            var customerItem = new CustomerItem();
            if (customer != null)
            {
                customerItem.CustomerId = customer.UserId;
                customerItem.FirstName = customer.FirstName;
                customerItem.LastName = customer.LastName;
                customerItem.Email = customer.Email;
                customerItem.Password = customer.Password;
                customerItem.PhoneNumber = customer.PhoneNo;
                customerItem.PostalCode = customer.PostalCode;
                customerItem.City = customer.City;
                customerItem.Provience = customer.Province;
                customerItem.Country = customer.Country;
            }
            return customerItem;

        }

        public List<ProductCategory> GetAllCategories()
        {
            var productCategories = from categories in _rBossEntities.Categories
                   select new ProductCategory
                   {

                       CategoryId = categories.CategoryId,
                       CategoryName = categories.CategoryName
                   };

            return productCategories.ToList();
        }
    }
}
